<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="__PUBLIC__/echarts/echarts.min.js"></script>
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="__CSS__/admin.css"  media="all">
</head>
<style>
    #tooltip{
        position:absolute;
        border:1px solid #ccc;
        background:#333;
        padding:2px;
        display:none;
        color:#fff;
    }
    .layui-table th, .layui-table td{
        padding: 9px 6px;
        text-align: center;
    }
    .operation-menu a{
        margin: 5px 0;!important;
    }
    .d_showkey{
        background-color: #9c9c9c
    }
</style>
<body>
<div class="layui-fluid main">
    <fieldset class="layui-elem-field site-demo-button" style="margin-top: 30px;border:0">
        <input type="hidden" id="poi_id" value="{$poi_id}">
        <div class="layui-upload" id="upload-thumb">
            <button type="button" class="layui-btn" id="thumb">上传图片</button>
        </div>
    </fieldset>
    <table class="layui-table" lay-even="" lay-skin="line">
        <colgroup>
            <col width="10%">
            <col width="10%">
            <col width="15%">
        </colgroup>
        <thead>

        <tr>
            <th>图片编号</th>
            <th>略缩图</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tbody">
        {volist name='imgArr' id='vo'}
        <tr>
            <td>{$vo.id}</td>
            <td>{if !empty($vo.img_src)}<a href="{$vo.img_src}" class="tooltip"><img src="{$vo.img_src}" width="60" height="60"></a>{/if}</td>
            <td>
                <a class="layui-btn layui-btn-xs layui-btn-normal showkey {if $vo.show=='0'}d_showkey{/if}" codeid="{$vo.id}" datakey="{$vo.show}" style="margin-right: 0;font-size:12px;">
                    {if $vo.show=='1'}已使用{else}未使用{/if}
                </a>
            </td>
        </tr>
        {/volist}
        </tbody>
    </table>
    <div>{$imgArr->render()}</div>

    <script src="__PUBLIC__/layui/layui.js"></script>
    <script src="__PUBLIC__/jquery/jquery.min.js"></script>

    <script>
        var message;
        layui.config({
            base: '__JS__/',
            version: '1.0.1'
        }).use(['app', 'message'], function() {
            var app = layui.app,
                $ = layui.jquery,
                layer = layui.layer;
            //将message设置为全局以便子页面调用
            message = layui.message;
            //主入口
            app.set({
                type: 'iframe'
            }).init();
        });

        $(function(){
            var x = 10;
            var y = 20;
            $(".tooltip").mouseover(function(e){
                var tooltip = "<div id='tooltip'><img src='"+ this.href +"' alt='头像' height='200'/>"+"<\/div>"; //创建 div 元素
                $("body").append(tooltip);  //把它追加到文档中
                $("#tooltip")
                    .css({
                        "top": (e.pageY+y) + "px",
                        "left":  (e.pageX+x)  + "px"
                    }).show("fast");    //设置x坐标和y坐标，并且显示
            }).mouseout(function(){
                $("#tooltip").remove();  //移除
            }).mousemove(function(e){
                $("#tooltip")
                    .css({
                        "top": (e.pageY+y) + "px",
                        "left":  (e.pageX+x)  + "px"
                    });
            });

            $(".tooltip").click(function(){
                return false;
            });
        })
    </script>
    <script>
        $(document).on('click', '.showkey', function() {
            var id = $(this).attr("codeid");
            var key = $(this).attr("datakey");
            var showkey = $(this);
            $.ajax({
                url:"{:url('admin/poi/changeImgShow')}",
                data:{
                    id:id,
                    key:key
                },
                type:'post',
                async: false,
                success:function(res) {
                    if(res.code == 1) {
                        if(key==1){
                            showkey.attr("datakey",0);
                            showkey.addClass("d_showkey");
                            showkey.html("未使用");
                        }else{
                            showkey.attr("datakey",1);
                            showkey.removeClass("d_showkey");
                            showkey.html("已使用");
                        }
                    }
                }
            })
            return false;
        });
    </script>
    <script>
        layui.use('upload', function(){
            var upload = layui.upload;
            //执行实例
            var uploadInst = upload.render({
                elem: '#thumb' //绑定元素
                ,url: "{:url('common/upload')}" //上传接口
                ,multiple: true
                ,accept:"images"
                ,size:"50000"
                ,done: function(result){
                    //上传完毕回调
                    layer.closeAll('loading');
                    if(result.code == 2) {
                        var poi_id = $("#poi_id").attr("value");
                        $.ajax({
                            url:"{:url('admin/poi/upImg')}",
                            data:{
                                poi_id:poi_id,
                                src:result.src
                            },
                            type:'post',
                            async: false,
                            success:function(res) {
                                if(res.code == 1) {
                                    var data = res.data;
                                    var html = "<tr><td>"+data.id+"</td><td><a href="+data.img_src+" class='tooltip'><img src="+data.img_src+" width='60' height='60'></a></td><td><a class='layui-btn layui-btn-xs layui-btn-normal showkey d_showkey' codeid='"+data.id+"' datakey='0' style='margin-right: 0;font-size:12px;'>未使用</a></td></tr>";

                                    $("#tbody").prepend(html);
                                    layer.msg(res.msg);
                                }
                            }
                        })
                    } else {
                        layer.msg(res.msg);
                    }
                }
                ,error: function(){
                    //请求异常回调
                    layer.closeAll('loading');
                    layer.msg("上传失败");
                    return false;
                }
                ,before:function () {
                    layer.load(2);
                }
            });
            return false;
        });
    </script>
</body>
</html>
